package ca.uwindsor;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class UpdatePassword extends Activity {


	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.update_password);
		
		Button add_but = (Button) findViewById(R.id.add_button);
		add_but.setOnClickListener(new View.OnClickListener(){
		public void onClick(View v) {
			final AlertDialog.Builder alert = new AlertDialog.Builder((UpdatePassword.this));
			EditText oldpass = (EditText) findViewById(R.id.oldpass);
			EditText newpass = (EditText) findViewById(R.id.newpass);
			EditText conpass = (EditText) findViewById(R.id.conpass);
			String pass = null;
			MyDBAdapter db = new MyDBAdapter(UpdatePassword.this);
			db.open();
			db.setDATABASE_TABLE("users");
			Cursor c = db.getAllEntries();
			if(oldpass.getEditableText().toString().trim().equals("") 
					|| newpass.getEditableText().toString().trim().equals("")
					|| conpass.getEditableText().toString().trim().equals(""))
			{
				db.close();
				alert.setMessage("All fields are required..");
				alert.setNegativeButton("OK", new OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {
						
					}

				});
				
				alert.show();
			}
			else if(c.moveToNext()){
				pass = c.getString(c.getColumnIndex("password"));
				db.close();
				if(pass.equals(oldpass.getEditableText().toString()))
					{
						if(newpass.getEditableText().toString().equals(conpass.getEditableText().toString()))
						{
							ContentValues cv = new ContentValues();
							cv.put("password", newpass.getEditableText().toString());
							db.open();
							db.setDATABASE_TABLE("users");
							db.updateEntry("_id", 1, cv);
							db.close();
							alert.setMessage("Passwords updated..");

							alert.setNegativeButton("OK", new OnClickListener() {

								public void onClick(DialogInterface dialog, int which) {
									startActivity(new Intent("ca.uwindsor.SUMMARY"));
								}

							});
							
							alert.show();
						}
						else
						{
							alert.setMessage("Passwords do not match..");

							alert.setNegativeButton("OK", new OnClickListener() {

								public void onClick(DialogInterface dialog, int which) {
									// TODO Auto-generated method stub
								}

							});
							
							alert.show();
						}
					}
				else
					{
					alert.setMessage("Wrong Password.. Please Try Again.");

					alert.setNegativeButton("OK", new OnClickListener() {

						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
						}

					});
					
					alert.show();
					}
			}
		}
		});
		
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		finish();
	}

	
}
